<?php

namespace app\api\controller;

use app\api\validate\OrderValidate;
use app\common\controller\Api;

class Order extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function add()
    {
        $data = $this->request->post() ;
        $orderValidate = new OrderValidate() ;
        if($orderValidate->check($data) == false){
            $this->error($orderValidate->getError());
        }
        $data['user_id'] = 1;

        $model = new \app\common\model\Order() ;
        $model->allowField(true)->save($data) ;
        $this->success('提交成功，后台审核中');
    }
    public function order_list()
    {
        $this->userId = 1 ;
        $status = $this->request->post('status' , 1) ;
        $page = $this->request->post('page' , 1 , 'intval') ;
        $list = \app\common\model\Order::
            field('id,price,title,img_url,type')
            ->where('check_status' , $status)
            ->where('user_id' , $this->userId)
            ->order('id desc')->page($page , 10)->select() ;
        $this->success('请求成功' ,$list);
    }
    public function user_order_detail()
    {
        $id = $this->request->post('id' , 0) ;
        $info = \app\common\model\Order::where('id'  , $id)->find() ;
        $this->success('请求成功' ,$info);
    }
    public function edit()
    {
        $data = $this->request->post() ;
        $orderValidate = new OrderValidate() ;
        if($orderValidate->check($data) == false){
            $this->error($orderValidate->getError());
        }
        $id = $this->request->post('id' , 0,'intval') ;
        unset($data['id']) ;
        $data['check_status'] = \app\common\model\Order::CHECK_STATUS_ING ;

        $model = new \app\common\model\Order() ;
        $model->allowField(true)->where('id' , $id)->save($data) ;
        $this->success('提交成功，后台审核中');
    }
}